<template>
  <div>
    <div class="container-div">
      <el-card
        class="box-card"
        style="padding-right: 10%; padding-left: 10%; margin-top: 20px"
      >
        <el-form
          ref="ruleForm"
          :model="form"
          :rules="rules"
          label-position="top"
          status-icon
          label-width="120px"
          @submit.prevent
        >
          <el-form-item label="图标" prop="logo">
            <single-upload v-model="form.logo" />
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input maxlength="20" v-model="form.name" />
          </el-form-item>
          <el-form-item label="说明" prop="remark">
            <el-input maxlength="40" v-model="form.remark" />
          </el-form-item>

          <el-form-item label="流程id" prop="uniqueId">
            <el-input
              disabled
              placeholder="流程唯一值，根据通过该字段发起流程"
              maxlength="40"
              v-model="form.uniqueId"
            >
              <template #append>
                <el-button @click="copyUniqueId" :icon="DocumentCopy" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="流程类型" prop="taskType">
            <el-select
              style="width: 100%"
              v-model="form.taskType"
              placeholder="请选择流程类型"
              @change="handSelectRule(form.taskType)"
            >
              <el-option
                v-for="item in taskTypeList"
                :key="item.id + ''"
                :label="item.label"
                :value="item.id + ''"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="对应规则" prop="ruleId" v-if="form.taskType">
            <el-select
              style="width: 100%"
              v-model="form.ruleId"
              placeholder="请选择对应规则"
              @change="handReplaceName(form.ruleId)"
            >
              <el-option
                v-for="item in ruleDataSource"
                :key="item.id + ''"
                :label="item.name"
                :value="item.id + ''"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="分组" prop="groupId">
            <el-select
              style="width: 100%"
              v-model="form.groupId"
              placeholder="请选择流程组"
            >
              <el-option
                v-for="item in groupList"
                :key="item.id + ''"
                :label="item.groupName"
                :value="item.id + ''"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="谁可以发起该流程（默认全员）" prop="rangeList">
            <select-show
              v-model:orgList="form.rangeList"
              type="org"
              :disable-select-children-dept="false"
              :multiple="true"
            ></select-show>
          </el-form-item>

          <el-form-item label="管理员" prop="admin">
            <select-show
              v-model:orgList="form.admin"
              type="user"
              :multiple="false"
            ></select-show>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, computed, watch, reactive } from "vue";

import { queryGroupList } from "../../api/group";
import {
  getVacationAll,
  getAllRemedyRuleInfoApi,
} from "../../../../api/vacation/rule";
import SingleUpload from "../Upload/SingleUpload.vue";

import type { FormInstance, FormRules } from "element-plus";
import selectShow from "../orgselect/selectAndShow.vue";
// import selectShow from "../tree/index.vue";

import { GroupVO } from "../../api/group/types";

import { useFlowStore } from "../../stores/flow";
import { DocumentCopy, FirstAidKit } from "@element-plus/icons-vue";
import { copyToBoard, getRandomId, isBlank } from "../../utils/objutil";

const ruleForm = ref();
const taskTypeList = ref([
  {
    label: "请假",
    id: 1,
  },
  {
    label: "补卡",
    id: 2,
  },
]);
const ruleDataSource = ref([]);

const validate = (f) => {
  ruleForm.value.validate((valid, fields) => {
    var arr = [];
    if (!valid) {
      for (var err in fields) {
        arr.push(fields[err][0].message);
      }
    }

    f(valid, arr);
  });
};

// 暴露方法和属性给父组件
defineExpose({ validate });
const rules = reactive<FormRules>({
  name: [
    { required: true, message: "请填写名称", trigger: "blur" },
    { min: 2, max: 20, message: "流程名称：2-20个字符", trigger: "blur" },
  ],
  uniqueId: [
    { required: true, message: "请填写流程id", trigger: "blur" },
    { min: 10, max: 50, message: "流程id：10-50个字符", trigger: "blur" },
  ],
  remark: [
    { required: false, message: "请填写描述", trigger: "blur" },
    { min: 2, max: 40, message: "描述：2-40个字符", trigger: "blur" },
  ],
  taskType: [
    {
      required: false,
      message: "请选择流程类型",
      trigger: "change",
    },
  ],
  ruleId: [
    {
      required: true,
      message: "请选择对应规则",
      trigger: "change",
    },
  ],
  groupId: [
    {
      required: true,
      message: "请选择分组",
      trigger: "change",
    },
  ],
  logo: [
    {
      required: true,
      message: "请上传图标",
      trigger: "change",
    },
  ],
  admin: [
    {
      required: true,
      message: "请选择管理员",
      trigger: "change",
    },
  ],
});

const groupList = ref<GroupVO[]>([]);

onMounted(() => {
  queryGroupList().then(({ data }) => {
    groupList.value = data;
  });
});

let props = defineProps({
  groupId: {
    type: String,
    default: undefined,
  },
});

watch(
  () => props.groupId,
  (val) => {
    if (!!val) {
      form.value.groupId = val;
    }
  }
);

let flowStore = useFlowStore();

var form = computed(() => {
  return flowStore.step1;
});
//复制唯一id
function copyUniqueId() {
  copyToBoard(form.value.uniqueId);
}
//生成新的唯一id
function produceNewUniqueId() {
  form.value.uniqueId = getRandomId();
}

/**
 * 选择类型获取规则
 * @param val
 */
const handSelectRule = (val) => {
  if (val == 1) {
    getVacationAll().then((res) => {
      ruleDataSource.value = res.data;
    });
  } else if (val == 2) {
    getAllRemedyRuleInfoApi().then((res) => {
      ruleDataSource.value = res.data;
    });
  }
};

/**
 * 根据规则修改名称
 * @param val
 */
const handReplaceName = (val) => {
  const name = ruleDataSource.value.filter((item) => item.id == val)[0].name;
  form.value.name = name;
};
</script>
<style scoped lang="less">
.container-div {
  width: 800px;
  margin-left: calc(50% - 400px);
  text-align: center;
}

.range {
  width: 100%;
  display: flex;
}

.rangeDiv {
  width: 100px;
  font-size: 14px;
  height: 30px;
  margin-right: 8px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
  padding-right: 20px;
  line-height: 30px;
  padding-left: 8px;
  position: relative;
}
.rangeDivIcon {
  position: absolute;
  top: 5px;
  right: 0;
  width: 20px;
  height: 20px;
  cursor: pointer;
}
</style>
